package com.chuantou.yuba.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.baidu.location.BDLocation;
import com.chuantou.yuba.R;
import com.chuantou.yuba.YBApp;
import com.chuantou.yuba.api.RequestManager;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.CityTO;
import com.chuantou.yuba.api.to.FishingShopDataTO;
import com.chuantou.yuba.api.to.FishingShopTO;
import com.chuantou.yuba.api.to.IdNameTO;
import com.chuantou.yuba.ui.LoginActivity;
import com.chuantou.yuba.ui.widget.CityPopupWindow;
import com.chuantou.yuba.ui.widget.FishingShopPopup;
import com.chuantou.yuba.ui.widget.NearByPopup;
import com.chuantou.yuba.ui.widget.NearByPopup.OnMenuSelectedListener;
import com.chuantou.yuba.ui.widget.UploadNearPopup;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.FragmentUtil;
import com.chuantou.yuba.util.UiUtil;

/**
 * 附近fragment
 * 
 * @author dl1001
 * 
 */
public class NearByFragment extends BaseFragment implements OnClickListener {
	private static final String KEY_FISHING_FRIEND_SECOND_ITEM = "fishing_friend_second_item2";
	private static final String KEY_FISHING_FRIEND_THIRD_ITEM = "fishing_friend_second_item3";
	private static final String KEY_FISHING_PLACE_SECOND_ITEM = "fishing_place_second_item2";
	private static final String KEY_FISHING_PLACE_THIRD_ITEM = "fishing_place_second_item3";
	private static final String KEY_FISHING_SHOP_SECOND_ITEM = "fishing_shop_second_item2";
	private static final String KEY_FISHING_SHOP_THIRD_ITEM = "fishing_shop_second_item3";
	private static final int TYPE_FISHING_FRIEND = 0;
	private static final int TYPE_FISHING_PLACE = 1;
	private static final int TYPE_FISHING_SHOP = 2;
	private static final LinkedHashMap<Integer, String> FIRST_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> DISTANCE_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> DISTANCE_MENU_ITEMS_2 = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> SEX_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> COST_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> TYPE_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	private static final LinkedHashMap<Integer, String> SHOP_MENU_ITEMS = new LinkedHashMap<Integer, String>();
	static {
		FIRST_MENU_ITEMS.put(TYPE_FISHING_FRIEND, "找钓友");
		FIRST_MENU_ITEMS.put(TYPE_FISHING_PLACE, "找钓场");
		FIRST_MENU_ITEMS.put(TYPE_FISHING_SHOP, "渔具店");

		DISTANCE_MENU_ITEMS.put(0, "全国");
		DISTANCE_MENU_ITEMS.put(2, "2km");
		DISTANCE_MENU_ITEMS.put(5, "5km");
		DISTANCE_MENU_ITEMS.put(8, "8km");

		DISTANCE_MENU_ITEMS_2.put(0, "距离");
		DISTANCE_MENU_ITEMS_2.put(8, "8km");
		DISTANCE_MENU_ITEMS_2.put(15, "15km");
		DISTANCE_MENU_ITEMS_2.put(30, "30km");
		DISTANCE_MENU_ITEMS_2.put(50, "50km");

		SEX_MENU_ITEMS.put(0, "不限");
		SEX_MENU_ITEMS.put(1, "女");
		SEX_MENU_ITEMS.put(2, "男");

		COST_MENU_ITEMS.put(0, "全部");
		COST_MENU_ITEMS.put(1, "免费");
		COST_MENU_ITEMS.put(2, "天塘");
		COST_MENU_ITEMS.put(3, "斤塘");

		TYPE_MENU_ITEMS.put(0, "全部");
		TYPE_MENU_ITEMS.put(1, "池塘");
		TYPE_MENU_ITEMS.put(2, "水库");
		TYPE_MENU_ITEMS.put(3, "江河");
		TYPE_MENU_ITEMS.put(4, "湖泊");
		TYPE_MENU_ITEMS.put(5, "海钓");
		TYPE_MENU_ITEMS.put(6, "其他");

		SHOP_MENU_ITEMS.put(1, "经销商");
		SHOP_MENU_ITEMS.put(2, "品牌商");
		SHOP_MENU_ITEMS.put(3, "网店");
	}

	private View mContentView;
	private TextView mFirstMenuView;
	private TextView mSecondMenuView;
	private TextView mThirdMenuView;
	private View mMenuLayout;
	// 默认钓友
	private int mCurrentType = TYPE_FISHING_FRIEND;
	private HashMap<String, Integer> mSelectedMenuItems = new HashMap<String, Integer>();
	private BDLocation mLocation;
	private NearFilterFragment<?> mCurrentFragment;
	private List<FishingShopTO> mPinpaiShops;
	private UploadNearPopup mPopup;
	private HashMap<String, String> mSelectedCity = new HashMap<String, String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		YBApp.get().startLocate(null);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		if (mContentView == null) {
			mLocation = YBApp.get().getLocation();
			mContentView = inflater.inflate(R.layout.fg_nearby, null);
			mFirstMenuView = (TextView) mContentView.findViewById(R.id.first_menu);
			mFirstMenuView.setOnClickListener(this);
			mSecondMenuView = (TextView) mContentView.findViewById(R.id.second_menu);
			mSecondMenuView.setOnClickListener(this);
			mThirdMenuView = (TextView) mContentView.findViewById(R.id.third_menu);
			mThirdMenuView.setOnClickListener(this);
			mMenuLayout = mContentView.findViewById(R.id.menu_layout);
			mContentView.findViewById(R.id.upload).setOnClickListener(this);
			IdNameTO to = new IdNameTO();
			to.setId(TYPE_FISHING_FRIEND);
			to.setName(getString(R.string.find_fishing_friends));
			switchMenu(to, DISTANCE_MENU_ITEMS, SEX_MENU_ITEMS);
			if (mLocation == null) {
				if (!isGPSOPen(mContext)) {
					showNoGPSDialog();
				} else {
					startGetLocation();
				}
			} else {
				switchContentView(TYPE_FISHING_FRIEND);
			}
			requestPinPaiFishingShop();
		}
		ViewGroup parentView = (ViewGroup) mContentView.getParent();
		if (parentView != null) {
			parentView.removeView(mContentView);
		}
		return mContentView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.first_menu:
			showMenuPopupWindow(mFirstMenuView);
			break;
		case R.id.second_menu:
			showMenuPopupWindow(mSecondMenuView);
			break;
		case R.id.third_menu:
			if (mCurrentType == TYPE_FISHING_FRIEND) {
				showMenuPopupWindow(mThirdMenuView);
			} else {
				showCityPopupWindow(mThirdMenuView);
			}
			break;
		case R.id.upload:
			AccountManager accountManager = AccountManager.getInstance(mContext);
			if (!accountManager.isLogined()) {
				UiUtil.showNoLoginDialog(this, -1, LoginActivity.ACTION_RETURN);
				return;
			}
			if (mPopup != null && mPopup.isShowing())
				return;
			mPopup = new UploadNearPopup(getActivity());
			ViewGroup decor = (ViewGroup) getActivity().getWindow().getDecorView();
			mPopup.showAtLocation(decor, Gravity.BOTTOM, 0, 0);
			break;
		default:
			break;
		}
	}

	private List<IdNameTO> map2List(LinkedHashMap<Integer, String> map) {
		List<IdNameTO> list = new ArrayList<IdNameTO>();
		for (Integer key : map.keySet()) {
			IdNameTO to = new IdNameTO();
			to.setId(key);
			to.setName(map.get(key));
			list.add(to);
		}
		return list;
	}

	private void showCityPopupWindow(TextView view) {
		mFirstMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		mSecondMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		mThirdMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		view.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_shrink, 0);
		CityPopupWindow popupWindow = new CityPopupWindow(getActivity());
		popupWindow.setOnCityClickListener(new CityPopupWindow.OnCityClickListener() {

			@Override
			public void onCityClick(CityTO city) {
				mThirdMenuView.setText(city.getName());
				mSelectedCity.put(mCurrentFragment.getClass().getName(), city.getName());
				mCurrentFragment.filter(UriHelper.PARAMS_STATE_ID, city.getPid(), false);
				mCurrentFragment.filter(UriHelper.PARAMS_CITY_ID, city.getId());
			}
		});
		popupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				mFirstMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
				mSecondMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
				mThirdMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
			}
		});
		popupWindow.showAsDropDown(mMenuLayout);
	}

	public void showMenuPopupWindow(TextView view) {
		if (UiUtil.isFastDoubleClick())
			return;
		mFirstMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		mSecondMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		mThirdMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
		view.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_shrink, 0);
		NearByPopup popup = null;
		switch (view.getId()) {
		case R.id.first_menu:
			popup = new NearByPopup(getActivity(), map2List(FIRST_MENU_ITEMS), mCurrentType);
			popup.setOnMenuSelectedListener(mFirstMenuListener);
			break;
		case R.id.second_menu:
			if (mCurrentType == TYPE_FISHING_FRIEND) {
				popup = new NearByPopup(getActivity(), map2List(DISTANCE_MENU_ITEMS), getSecondMenuItemSelectedID(mCurrentType, DISTANCE_MENU_ITEMS));
			} else if (mCurrentType == TYPE_FISHING_PLACE) {
				popup = new NearByPopup(getActivity(), map2List(DISTANCE_MENU_ITEMS_2), getSecondMenuItemSelectedID(mCurrentType, DISTANCE_MENU_ITEMS_2));
			} else if (mCurrentType == TYPE_FISHING_SHOP) {
				popup = new NearByPopup(getActivity(), map2List(SHOP_MENU_ITEMS), getSecondMenuItemSelectedID(mCurrentType, SHOP_MENU_ITEMS));
			}
			if (popup != null) {
				popup.setOnMenuSelectedListener(mSecondMenuListener);
			}
			break;
		case R.id.third_menu:
			if (mCurrentType == TYPE_FISHING_FRIEND) {
				popup = new NearByPopup(getActivity(), map2List(SEX_MENU_ITEMS), getThirdMenuItemSelectedID(mCurrentType, SEX_MENU_ITEMS));
			} else if (mCurrentType == TYPE_FISHING_PLACE) {
				popup = new NearByPopup(getActivity(), map2List(TYPE_MENU_ITEMS), getThirdMenuItemSelectedID(mCurrentType, TYPE_MENU_ITEMS));
			} else if (mCurrentType == TYPE_FISHING_SHOP) {
				popup = new NearByPopup(getActivity(), map2List(DISTANCE_MENU_ITEMS), getThirdMenuItemSelectedID(mCurrentType, DISTANCE_MENU_ITEMS));
			}
			if (popup != null) {
				popup.setOnMenuSelectedListener(mThirdMenuListener);
			}
			break;
		default:
			break;
		}
		if (popup != null) {
			popup.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss() {
					mFirstMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
					mSecondMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
					mThirdMenuView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_expand, 0);
				}
			});
			popup.showAsDropDown(mMenuLayout);
		}
	}

	private OnMenuSelectedListener mFirstMenuListener = new OnMenuSelectedListener() {

		@Override
		public void onItemSelected(IdNameTO to) {
			mCurrentType = to.getId();
			mThirdMenuView.setEnabled(true);
			if (to.getId() == TYPE_FISHING_FRIEND) {
				switchMenu(to, DISTANCE_MENU_ITEMS, SEX_MENU_ITEMS);
			} else if (to.getId() == TYPE_FISHING_PLACE) {
				switchMenu(to, DISTANCE_MENU_ITEMS_2, TYPE_MENU_ITEMS);
			} else if (to.getId() == TYPE_FISHING_SHOP) {
				switchMenu(to, SHOP_MENU_ITEMS, DISTANCE_MENU_ITEMS);
			}
			switchContentView(to.getId());
		}
	};

	private OnMenuSelectedListener mSecondMenuListener = new OnMenuSelectedListener() {

		@Override
		public void onItemSelected(IdNameTO to) {
			if (mCurrentType == TYPE_FISHING_FRIEND) {
				mSecondMenuView.setText(to.getName());
				mSelectedMenuItems.put(KEY_FISHING_FRIEND_SECOND_ITEM, to.getId());
				mCurrentFragment.filter(UriHelper.PARAMS_DISTANCE, String.valueOf(to.getId()));
			} else if (mCurrentType == TYPE_FISHING_PLACE) {
				mSecondMenuView.setText(to.getName());
				mSelectedMenuItems.put(KEY_FISHING_PLACE_SECOND_ITEM, to.getId());
				mCurrentFragment.filter(UriHelper.PARAMS_DISTANCE, String.valueOf(to.getId()));
			} else if (mCurrentType == TYPE_FISHING_SHOP) {
				if (to.getId() == 3) {
					// 网店3级菜单不可用
					mThirdMenuView.setEnabled(false);
					mThirdMenuView.setText(R.string.whole_country);
				} else {
					// 经销商和品牌商3级菜单可使用
					mThirdMenuView.setEnabled(true);
				}
				// 经销商和网店在当前界面更新，品牌商弹出popupwindow显示
				if (to.getId() == 1 || to.getId() == 3) {
					mSecondMenuView.setText(to.getName());
					mSelectedMenuItems.put(KEY_FISHING_SHOP_SECOND_ITEM, to.getId());
					mCurrentFragment.filter(UriHelper.PARAMS_TYPE, String.valueOf(to.getId()));
				} else {
					// 弹出品牌商对话框
					if (!UiUtil.isFastDoubleClick()) {
						FishingShopPopup popup = new FishingShopPopup(getActivity(), mPinpaiShops);
						popup.showAsDropDown(mMenuLayout);
					}
				}
			}
		}
	};

	private OnMenuSelectedListener mThirdMenuListener = new OnMenuSelectedListener() {

		@Override
		public void onItemSelected(IdNameTO to) {
			mThirdMenuView.setText(to.getName());
			mThirdMenuView.setEnabled(true);
			if (mCurrentType == TYPE_FISHING_FRIEND) {
				mSelectedMenuItems.put(KEY_FISHING_FRIEND_THIRD_ITEM, to.getId());
				mCurrentFragment.filter(UriHelper.PARAMS_SEX, String.valueOf(to.getId()));
			} else if (mCurrentType == TYPE_FISHING_PLACE) {
				mSelectedMenuItems.put(KEY_FISHING_PLACE_THIRD_ITEM, to.getId());
				mCurrentFragment.filter(UriHelper.PARAMS_PLACE_TYPE, String.valueOf(to.getId()));
			} else if (mCurrentType == TYPE_FISHING_SHOP) {

			}
		}
	};

	private void switchContentView(int type) {
		String tag = String.valueOf(type);
		FragmentManager fragmentManager = getChildFragmentManager();
		FragmentUtil.hideAllFragment(fragmentManager);
		Fragment fragment = fragmentManager.findFragmentByTag(tag);
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (fragment == null) {
			switch (type) {
			case TYPE_FISHING_FRIEND:
				fragment = new NearFishingFriendFragment();
				break;
			case TYPE_FISHING_PLACE:
				fragment = new NearFishingPlaceFragment();
				break;
			case TYPE_FISHING_SHOP:
				fragment = new NearFishingShopFragment();
				break;
			default:
				fragment = new NearFishingFriendFragment();
				break;
			}
			transaction.add(R.id.near_content, fragment, tag);
		} else {
			transaction.show(fragment);
		}
		mCurrentFragment = (NearFilterFragment<?>) fragment;
		transaction.commitAllowingStateLoss();
	}

	private void switchMenu(IdNameTO to, LinkedHashMap<Integer, String> secondMenuItems, LinkedHashMap<Integer, String> thirdMenuItems) {
		mFirstMenuView.setText(to.getName());
		Integer secondID = getSecondMenuItemSelectedID(to, secondMenuItems);
		String secondMenuValue = secondMenuItems.get(secondID);
		mSecondMenuView.setText(secondMenuValue);
		Integer thirdID = getThirdMenuItemSelectedID(to, thirdMenuItems);
		String thirdMenuValue = thirdMenuItems.get(thirdID);
		mThirdMenuView.setText(thirdMenuValue);
		if (mCurrentType == TYPE_FISHING_SHOP) {
			// 网店
			if (secondID == 3) {
				mThirdMenuView.setEnabled(false);
				mThirdMenuView.setText(R.string.whole_country);
			} else {
				String cityName = mSelectedCity.get(NearFishingShopFragment.class.getName());
				cityName = TextUtils.isEmpty(cityName) ? getString(R.string.whole_country) : cityName;
				mThirdMenuView.setEnabled(true);
				mThirdMenuView.setText(cityName);
			}
		} else if (mCurrentType == TYPE_FISHING_PLACE) {
			String cityName = mSelectedCity.get(NearFishingPlaceFragment.class.getName());
			cityName = TextUtils.isEmpty(cityName) ? getString(R.string.whole_country) : cityName;
			mThirdMenuView.setText(cityName);
			mThirdMenuView.setEnabled(true);
		} else {
			mThirdMenuView.setEnabled(true);
		}
	}

	public Integer getSecondMenuItemSelectedID(IdNameTO to, LinkedHashMap<Integer, String> secondMenuItems) {
		return getSecondMenuItemSelectedID(to.getId(), secondMenuItems);
	}

	public Integer getSecondMenuItemSelectedID(int type, LinkedHashMap<Integer, String> secondMenuItems) {
		Integer id = 0;
		if (type == TYPE_FISHING_FRIEND) {
			id = mSelectedMenuItems.get(KEY_FISHING_FRIEND_SECOND_ITEM);
		} else if (type == TYPE_FISHING_PLACE) {
			id = mSelectedMenuItems.get(KEY_FISHING_PLACE_SECOND_ITEM);
		} else if (type == TYPE_FISHING_SHOP) {
			id = mSelectedMenuItems.get(KEY_FISHING_SHOP_SECOND_ITEM);
		}
		if (id == null) {
			for (Integer key : secondMenuItems.keySet()) {
				id = key;
				break;
			}
		}
		return id;
	}

	public Integer getThirdMenuItemSelectedID(IdNameTO to, LinkedHashMap<Integer, String> thirdMenuItems) {
		return getThirdMenuItemSelectedID(to.getId(), thirdMenuItems);
	}

	public Integer getThirdMenuItemSelectedID(int type, LinkedHashMap<Integer, String> thirdMenuItems) {
		Integer id = 0;
		if (type == TYPE_FISHING_FRIEND) {
			id = mSelectedMenuItems.get(KEY_FISHING_FRIEND_THIRD_ITEM);
		} else if (type == TYPE_FISHING_PLACE) {
			id = mSelectedMenuItems.get(KEY_FISHING_PLACE_THIRD_ITEM);
		} else if (type == TYPE_FISHING_SHOP) {
			id = mSelectedMenuItems.get(KEY_FISHING_SHOP_THIRD_ITEM);
		}
		if (id == null) {
			for (Integer key : thirdMenuItems.keySet()) {
				id = key;
				break;
			}
		}
		return id;
	}

	private void requestPinPaiFishingShop() {
		String url = UriHelper.getNearFishingShopUrl();
		HashMap<String, String> params = new HashMap<String, String>();
		params.put(UriHelper.PARAMS_DISTANCE, String.valueOf("0"));
		params.put(UriHelper.PARAMS_PAGE_SIZE, String.valueOf(10000));
		params.put(UriHelper.PARAMS_PAGE_NO, String.valueOf(1));
		// 取品牌商
		params.put(UriHelper.PARAMS_TYPE, "2");
		GsonRequest<FishingShopDataTO> request = new GsonRequest<FishingShopDataTO>(Method.POST, url, new Listener<FishingShopDataTO>() {

			@Override
			public void onResponse(FishingShopDataTO response) {
				mPinpaiShops = response.getShops();
			}
		}, null, params, FishingShopDataTO.class);
		RequestManager.startRequest(mContext, request);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mNoGPSDialog != null && mNoGPSDialog.isShowing() && isGPSOPen(mContext)) {
			mNoGPSDialog.dismiss();
			startGetLocation();
		}
	}

	private AlertDialog mNoGPSDialog = null;

	private void showNoGPSDialog() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setTitle(R.string.no_gprs_title);
		builder.setMessage(R.string.no_gprs_content);
		builder.setCancelable(false);
		builder.setPositiveButton(R.string.go_setting, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent();
				intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				try {
					startActivity(intent);
				} catch (ActivityNotFoundException ex) {
					intent.setAction(Settings.ACTION_SETTINGS);
					try {
						startActivity(intent);
					} catch (Exception e) {
					}
				}
			}
		});
		mNoGPSDialog = builder.create();
		mNoGPSDialog.show();
	}

	private ProgressDialog mGetLocationDialog = null;

	private void startGetLocation() {
		mGetLocationDialog = new ProgressDialog(getActivity());
		mGetLocationDialog.setMessage(getString(R.string.getting_current_location));
		mGetLocationDialog.setCancelable(false);
		mGetLocationDialog.show();
		new AsyncTask<Void, Void, BDLocation>() {
			private CountDownLatch mLatch = new CountDownLatch(1);

			@Override
			protected BDLocation doInBackground(Void... params) {
				BDLocation location = YBApp.get().getLocation();
				try {
					int waitCount = 0;
					while (location == null) {
						mLatch.await(1, TimeUnit.SECONDS);
						location = YBApp.get().getLocation();
						// 10秒后无结果直接返回
						if (waitCount >= 10) {
							break;
						}
						waitCount++;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return location;
			}

			protected void onPostExecute(BDLocation location) {
				if (getActivity() == null)
					return;
				mGetLocationDialog.dismiss();
				if (location == null) {
					showCannotGetLocationDialog();
				} else {
					mLocation = location;
					switchContentView(TYPE_FISHING_FRIEND);
				}
			};

		}.execute();
	}

	private void showCannotGetLocationDialog() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setMessage(R.string.cannot_get_location_title);
		builder.setCancelable(false);
		builder.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				startGetLocation();
			}
		});
		builder.create().show();
	}

	private boolean isGPSOPen(final Context context) {
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		// 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
		boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
		boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		if (gps || network) {
			return true;
		}

		return false;
	}

	@Override
	public void onDestroy() {
		if (mNoGPSDialog != null && mNoGPSDialog.isShowing()) {
			mNoGPSDialog.dismiss();
		}
		mSelectedCity.clear();
		YBApp.get().stopLocate(false);
		super.onDestroy();
	}
}
